package org.shan.lis;


import com.alibaba.fastjson.JSON;

import org.shan.lis.common.model.PersonResult;
import org.shan.lis.shy.ShySerial;
import org.shan.mq.client.NettyMqClient;
import org.shan.mq.client.listener.TopicListener;
import org.shan.mq.client.listener.TopicListenerAbstract;
import org.shan.mq.client.listener.TopicListenerManager;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by amanda.shan on 2019-07-05.
 */
public class LisClient extends NettyMqClient{

    private ShySerial lisSerialHandler;

    private LisFrame lisFrame;

    public LisClient() throws Exception {
        super("47.103.101.96:1883");

        // 监听主题
        topicListener.addTopicFilter("shydata");
        TopicListenerManager.INSTANCE.add(topicListener);

        lisFrame = new LisFrame();
        lisFrame.setVisible(true);

        start();
        connectAndSub();

        new Thread(){
            @Override
            public void run() {
                while (true){

                    try {
                        ping();

                        Thread.sleep(20000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    TopicListener topicListener = new TopicListenerAbstract(){

        @Override
        public void reavieBytes(String topic, byte[] bytes) {

            System.out.println("收到消息:"+topic+"   "+new String(bytes, Charset.forName("UTF-8")));

            showData(JSON.parseArray(new String(bytes), PersonResult.class));

        }

        @Override
        public void close() {
            System.out.println("服务器断开连接");
            try {
                connectAndSub();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    public void showData(List<PersonResult> results){

        String[][] strings1 = new String[0][];
        if (results != null) {
            List<String[]> list = new ArrayList<>();
            for (int i = 0; i < results.size(); ++i) {

                PersonResult result = results.get(i);

                for (int j = 0; j < result.getSampleRedults().size(); j++) {

                    String[] strings = new String[5];
                    list.add(strings);

                    strings[0] = result.getPerson().getId();
                    strings[1] =
                            result.getSampleRedults().get(j).getSample().getId();
                    strings[2] = result.getSampleRedults().get(j).getSample().getChannel();
                    strings[3] = result.getSampleRedults().get(j).getResult().getResult();
                    strings[4] = result.getSampleRedults().get(j).getResult().getTime();
                }


            }
            strings1 = list.toArray(new String[0][]);
        }

        lisFrame.refresh(strings1);
    }

    private void connectAndSub() throws Exception {

        // 连接
        boolean connect = connect("luoling", "medgen2011", 100000);
        System.out.println("连接:"+connect);

        // 监听
        boolean subscribe = subscribe("shydata");
        System.out.println("监听:"+subscribe);
    }


    public static void main(String[] args) throws Exception {

        LisClient lisClient = new LisClient();

    }
}
